﻿using AutoMapper;
using DFGK.IService;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Dto;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class BackErpService : BaseService, IBackErpService
    {
        private readonly HttpClientHelper _httpClientHelper;
        public BackErpService(IMapper mapper, HttpClientHelper httpClientHelper) : base(mapper)
        {
            _httpClientHelper = httpClientHelper;
        }
        public async Task<bool> BackERPHttpClient(string path, List<ReceivingOrderDetailDto> dto)
        {
            if (dto != null)
            {

                try
                {
                    string jsonData = JsonConvert.SerializeObject(dto);
                    string targeUrl = "http://59.110.231.187:9883";
                    string Url = targeUrl + path;
                    // 调用 PostAsync 发送数据
                    string response = await _httpClientHelper.PostAsync(
                        url: Url,
                        body: jsonData,
                        bodyMediaType: "application/json",  // 明确指定 JSON 类型
                        headers: new Dictionary<string, string>
                        {
                    { "X-Api-Key", "your-api-key" }  // 可选：添加认证头
                        }
                    );

                    // 根据 response 处理返回结果，假设 response 为状态码或其他结果
                    if (!string.IsNullOrEmpty(response) && response.Contains("成功"))
                    {
                        return true;  // 假设返回值包含 "成功" 表示成功
                    }
                }
                catch (Exception ex)
                {
                    // 捕获异常并记录错误信息
                    Console.WriteLine($"同步过程中发生错误: {ex.Message}");
                }
            }

            // 发生异常或返回空时返回失败
            return false;
        }
    }
}
